package br.com.arduino.view;

import br.com.arduino.comunicacao.*;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class TelaLamapadas {

	private JFrame frame;

	SerialCom sc = new SerialCom();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaLamapadas window = new TelaLamapadas();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public TelaLamapadas() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JButton btnLiga = new JButton("Ligar");
		btnLiga.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {				


				ligaLampada();
				sc.fechaPortaSerial();
			}

		});
		btnLiga.setFont(new Font("Tahoma", Font.BOLD, 18));
		btnLiga.setBounds(10, 71, 197, 79);
		frame.getContentPane().add(btnLiga);

		JButton btnDelisga = new JButton("Desligar");
		btnDelisga.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				apagaLampada();
				//sc.fechaPortaSerial();



			}

			private void apagaLampada() {
				byte[] data = new byte[1];
				data[0] = 'D';
				sc.abrePortaSerial();
				sc.configuraPorta();
				sc.enviarDados(data[0]);
				//sc.fechaPortaSerial();
			}
		});
		btnDelisga.setFont(new Font("Tahoma", Font.BOLD, 18));
		btnDelisga.setBounds(227, 71, 197, 79);
		frame.getContentPane().add(btnDelisga);
	}

	private void ligaLampada() {
		byte[] data = new byte[1];
		data[0] = 'L';
		sc.abrePortaSerial();
		sc.configuraPorta();
		sc.enviarDados(data[0]);
		//sc.fechaPortaSerial();
	}
}
